"""
AI集成模块数据模型定义

作者: heruopu
© 2025 Heruopu. All rights reserved.
"""

from django.db import models
from projects.models import Project, Version
from cases.models import TestCase

class AITemplate(models.Model):
    """AI提示词模板模型"""
    name = models.CharField(max_length=200, verbose_name='模板名称')
    content = models.TextField(verbose_name='模板内容')
    project_type = models.CharField(max_length=50, null=True, blank=True, verbose_name='适用项目类型')
    description = models.TextField(blank=True, null=True, verbose_name='模板描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_default = models.BooleanField(default=False, verbose_name='是否默认模板')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'AI提示词模板'
        verbose_name_plural = 'AI提示词模板管理'
        ordering = ['-created_at']

class RequirementAnalysis(models.Model):
    """需求分析模型"""
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='requirement_analyses', verbose_name='所属项目')
    version = models.ForeignKey(Version, on_delete=models.CASCADE, related_name='requirement_analyses', verbose_name='所属版本', null=True, blank=True)
    original_content = models.TextField(verbose_name='原始需求内容')
    analysis_result = models.TextField(verbose_name='分析结果')
    modules_count = models.IntegerField(default=0, verbose_name='模块数量')
    requirements_count = models.IntegerField(default=0, verbose_name='需求数量')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    analyzed_by = models.CharField(max_length=100, null=True, blank=True, verbose_name='分析人')
    document_name = models.CharField(max_length=255, null=True, blank=True, verbose_name='文档名称')
    document_type = models.CharField(max_length=100, null=True, blank=True, verbose_name='文档类型')

    def __str__(self):
        return f'{self.project.name} 需求分析'

    class Meta:
        verbose_name = '需求分析'
        verbose_name_plural = '需求分析管理'
        ordering = ['-created_at']

class AIGenerationLog(models.Model):
    """AI生成日志模型"""
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='ai_generation_logs', verbose_name='所属项目')
    version = models.ForeignKey(Version, on_delete=models.CASCADE, related_name='ai_generation_logs', verbose_name='所属版本')
    prompt = models.TextField(verbose_name='提示词')
    generated_cases = models.IntegerField(default=0, verbose_name='生成用例数量')
    is_success = models.BooleanField(default=True, verbose_name='是否成功')
    error_message = models.TextField(blank=True, null=True, verbose_name='错误信息')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    created_by = models.CharField(max_length=100, null=True, blank=True, verbose_name='创建人')
    ai_model = models.CharField(max_length=100, default='deepseek-r1', verbose_name='AI模型')
    generation_time = models.IntegerField(default=0, verbose_name='生成耗时(秒)')

    def __str__(self):
        return f'{self.project.name} - {self.version.version_number} AI生成日志'

    class Meta:
        verbose_name = 'AI生成日志'
        verbose_name_plural = 'AI生成日志管理'
        ordering = ['-created_at']

class GeneratedTestCaseMapping(models.Model):
    """生成用例映射模型"""
    generation_log = models.ForeignKey(AIGenerationLog, on_delete=models.CASCADE, related_name='case_mappings', verbose_name='生成日志')
    test_case = models.OneToOneField(TestCase, on_delete=models.CASCADE, related_name='generation_mapping', verbose_name='测试用例')
    is_approved = models.BooleanField(default=False, verbose_name='是否已审核')

    def __str__(self):
        return f'{self.generation_log} -> {self.test_case.case_id}'

    class Meta:
        verbose_name = '生成用例映射'
        verbose_name_plural = '生成用例映射管理'
